package com.lg.atp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lg.atp.common.Constants;
import com.lg.atp.entity.TblAppInfo;
import com.lg.atp.entity.TblCaseInfo;
import com.lg.atp.entity.TblCaseStep;
import com.lg.atp.sercurity.SecurityUserHolder;
import com.lg.atp.service.IAppInfoService;
import com.lg.atp.service.ICaseService;
import com.lg.atp.service.IControlService;
import com.lg.atp.service.IParaService;

@RestController
@RequestMapping("case")
public class CaseController {

	private static Log log = LogFactory.getLog(CaseController.class);
	@Autowired
	private ICaseService iCaseService;
	@Autowired
	private IAppInfoService iAppInfoService;
	@Autowired
	private IParaService iParaService;
	@Autowired
	private IControlService iControlService;
	
	
	@GetMapping("index")
	public ModelAndView toCaseIndex() {
		ModelAndView view = new ModelAndView();
		view.setViewName("caseList");
		return view;
	}
	
	@GetMapping("toAdd")
	public ModelAndView toAdd() {
		ModelAndView view = new ModelAndView();
		List<TblAppInfo> applist = iAppInfoService.selectEditedApp();
		view.addObject("applist", applist);
		view.setViewName("case-add");
		return view;
	}
	
	@GetMapping("toEdit")
	public ModelAndView toEdit(Long caseId) {
		ModelAndView view = new ModelAndView();
		List<TblAppInfo> applist = iAppInfoService.selectEditedApp();
		view.addObject("applist", applist);
		view.addObject("caseId", caseId);
		view.setViewName("case-edit");
		return view;
	}
	
	@GetMapping("selectCtrl")
	public ModelAndView selectControl(Long appId,String types,String locateTypes,Integer index,Integer selectType,Long controlId) {
		ModelAndView view = new ModelAndView();
		view.setViewName("objectTree-layer");
		view.addObject("appId", appId);
		view.addObject("types", types);
		view.addObject("locateTypes", locateTypes);
		view.addObject("index", index);
		view.addObject("selectType", selectType);
		view.addObject("controlId", controlId);
		return view;
	}
	
	//选择数据页面
	@GetMapping("selectData")
	public ModelAndView selectData(Integer index,Integer selectType,Integer operateData,Integer operateRule) {
		ModelAndView view = new ModelAndView();
		view.setViewName("para-sel");
		view.addObject("index", index);
		view.addObject("selectType", selectType);
		view.addObject("operateData", operateData);
		view.addObject("operateRule", operateRule);
		return view;
	}
	
	//存入数据页面
	@GetMapping("crData")
	public ModelAndView crData(Integer index,Integer selectType,Integer operateData) {
		ModelAndView view = new ModelAndView();
		view.setViewName("para-cr");
		view.addObject("index", index);
		view.addObject("selectType", selectType);
		view.addObject("operateData", operateData);
		return view;
	}
	
	//运算页面
	@GetMapping("opData")
	public ModelAndView opData(Integer index,Integer selectType,String operateData) {
		ModelAndView view = new ModelAndView();
		view.setViewName("para-op");
		view.addObject("index", index);
		view.addObject("selectType", selectType);
		view.addObject("operateData", operateData);
		return view;
	}
	
	@GetMapping("toShow")
	public ModelAndView toShow(Long caseId) {
		ModelAndView view = new ModelAndView();
		view.addObject("caseId", caseId);
		view.setViewName("show");
		return view;
	}
	
	@GetMapping("toImp")
	public ModelAndView toImp(Long appId) {
		ModelAndView view = new ModelAndView();
		view.addObject("appId", appId);
		view.setViewName("case-radio");
		return view;
	}
	
	@PostMapping("list")
	public Map<String,Object> getCaseList(Integer pageIndex,Integer pageSize){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("pageIndex",pageIndex);
			map.put("pageSize", pageSize);
			PageInfo<TblCaseInfo> list = iCaseService.selectAllCase(map);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取案例列表失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取案例列表失败");
		}
		return result;
	}
	
	@PostMapping("laylist")
	public Map<String,Object> getSelCaseList(Integer pageIndex,Integer pageSize,String caseName,Long appId){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("pageIndex",pageIndex);
			map.put("pageSize", pageSize);
			map.put("caseName", caseName);
			map.put("appId", appId);
			PageInfo<TblCaseInfo> list = iCaseService.selectCaseByPara(map);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取案例列表失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取案例列表失败");
		}
		return result;
	}
	
	
	
	@PostMapping("getCaseById")
	public Map<String,Object> getCaseById(Long caseId){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			TblCaseInfo casedtl = iCaseService.getCaseById(caseId);
			JSONObject paraJson = iParaService.selectParaName();
			JSONObject controlJson = iControlService.selectControlName();
			
			result.put("data", casedtl);
			result.put("paraJson", paraJson);
			result.put("controlJson", controlJson);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取案例详情失败:"+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取案例详情失败");
		}
		
		return result;
		
	}
	
	
	@PostMapping("getStepByCaseId")
	public Map<String,Object> getStepByCaseId(Long caseId){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			List<TblCaseStep> stepList = iCaseService.getStepByCaseId(caseId);
			JSONObject paraJson = iParaService.selectParaName();
			JSONObject controlJson = iControlService.selectControlName();
			
			result.put("data", stepList);
			result.put("paraJson", paraJson);
			result.put("controlJson", controlJson);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("引入案例步骤失败:"+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "引入案例步骤失败");
		}
		
		return result;
		
	}
	
	
	@PostMapping("saveCase")
	public Map<String,Object> saveCase(String caseJson,String stepJson){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			TblCaseInfo caseInfo = JSONObject.parseObject(caseJson, TblCaseInfo.class);
			List<TblCaseStep> stepList = JSONObject.parseArray(stepJson, TblCaseStep.class);
			caseInfo.setCreateUserId(SecurityUserHolder.getCurrentUser().getId());
			caseInfo.setChangeUserId(SecurityUserHolder.getCurrentUser().getId());
			caseInfo.setStepList(stepList);
			Long caseId = iCaseService.saveCase(caseInfo);
			result.put("caseId",caseId);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("保存案例失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "案例保存失败");
		}
		return result;
	}
	
	
	@PostMapping("updateCase")
	public Map<String,Object> updateCase(String caseJson,String stepJson){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			TblCaseInfo caseInfo = JSONObject.parseObject(caseJson, TblCaseInfo.class);
			List<TblCaseStep> stepList = JSONObject.parseArray(stepJson, TblCaseStep.class);
			caseInfo.setStepList(stepList);
			iCaseService.updateCase(caseInfo);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("保存案例失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "案例保存失败");
		}
		return result;
	}
}
